// import consts from "../../../utils/consts.js"

const getCourses = () => {
  var courses = wx.getStorageSync("courses")
  if (!courses) {
    courses = "[]"
    return JSON.parse(courses);
  } else {
    return JSON.parse(courses);
  }
}

const setCourses = (courses) => {
  if (!courses) {
    wx.setStorageSync("courses", "[]")
  } else {
    wx.setStorageSync("courses", JSON.stringify(courses))
  }
}

/**
 * 根据id获取课程信息
 */
const getCourseById = (id) => {
  var courses = getCourses()
  for (var i in courses) {
    var c = courses[i]
    if (c.id == id) {
      return c
    }
  }
  return null
}



const parseCourse = (data) => {
  var course = {
    id: data.id,
    openid: data.openid,
    title: data.title,
    week: data.week,
    stimeStr: data.stime,
    stime: timeToFloat(data.stime),
    duration: data.duration,
    l_name: data.l_name,
    l_address: data.l_address,
    l_latitude: data.l_latitude,
    l_longitude: data.l_longitude
  }
  return course
}

const parseCourses = (data) => {
  var courses = []
  for (var i in data) {
    courses.push(parseCourse(data[i]))
  }
  console.log(courses)
  return courses
}

const timeToFloat = (time) => {
  var times = time.split(":")
  var hour = Number(times[0])
  var min = Number(times[1])
  return hour + min / 60
}

module.exports = {
  getCourses,
  setCourses,
  getCourseById,
  parseCourse,
  parseCourses
}